<?php

namespace App\Http\Controllers;

//use Illuminate\Http\Request;
use Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\Book;
use App\Models\UsersReg;
use App\Models\BooksComments;
use App\Models\Category as cat;
use App\Models\BookTempstorage;
use App\Models\BooksReview;
use App\Models\BooksPingfen;
use App\Models\PingfenInfo;
use Session;
use Redirect;

class BookPingfenController extends Controller
{

    public function savereadwant()
    {
        //想读 评分
        $uid = Session::get('nameid');
        $bookid = request::input('bookid-want');
        $user = BooksPingfen::where('uid', $uid)->where('book_id', $bookid)->count();
        //会员评论过这本书就更新信息，没有评论过这本书就添加信息
        if($user == 0) {
            // 添加
            $bookspingfen = new BooksPingfen;
            $bookspingfen->book_id     = request::input('bookid-want');
            $bookspingfen->uid         = $uid;
            $bookspingfen->rating      = 0;
            $bookspingfen->memo        = request::input('memo-want');
            if((request::input('private-want'))) {
            //判断是否自己所见
                $bookspingfen->private = 1;
            }
            else {
                $bookspingfen->private = 0;
            }
            $bookspingfen->read_state  = 1;  // 1想读 2在读 3读过
            $bookspingfen->add_time    = time();
            $bookspingfen->save();
        }
        else {
            // 更新
            $bookspingfen = BooksPingfen::where('uid', $uid)->where('book_id',$bookid)->first();
            //$bookspingfen->book_id     = request::input('bookid-want');
            //$bookspingfen->uid         = $uid;
            $bookspingfen->rating      = 0;
            $bookspingfen->memo        = request::input('memo-want');
            if((request::input('private-want'))) {
            //判断是否自己所见
            $bookspingfen->private = 1;
            }
            else {
            $bookspingfen->private = 0;
            }
            $bookspingfen->read_state  = 1;  // 1想读 2在读 3读过
            $bookspingfen->update_time    = time();
            $bookspingfen->save();
        }

        //$bookid = $bookspingfen->book_id;
        return Redirect::to('info/'. $bookid);
    }

    public function savereading()
    {
        //在读 评分
        $uid = Session::get('nameid');
        $bookid = request::input('bookid-reading');
        $user = BooksPingfen::where('uid', $uid)->where('book_id', $bookid)->count();
        //会员评论过这本书就更新信息，没有评论过这本书就添加信息
        if($user == 0) {
            // 添加
            $bookspingfen = new BooksPingfen;
            $bookspingfen->book_id     = request::input('bookid-reading');
            $bookspingfen->uid         = $uid;
            $bookspingfen->rating      = request::input('rating-reading');
            $bookspingfen->memo        = request::input('memo-reading');
            if((request::input('private-reading'))) {
            //判断是否自己所见
                $bookspingfen->private = 1;
            }
            else {
                $bookspingfen->private = 0;
            }
            $bookspingfen->read_state  = 2;  // 1想读 2在读 3读过
            $bookspingfen->add_time    = time();
            $bookspingfen->save();
        }
        else {
            // 更新
            $bookspingfen = BooksPingfen::where('uid', $uid)->where('book_id',$bookid)->first();
            //$bookspingfen->book_id     = request::input('bookid-reading');
            //$bookspingfen->uid         = $uid;
            $bookspingfen->rating      = request::input('rating-reading');
            $bookspingfen->memo        = request::input('memo-reading');
            if((request::input('private-reading'))) {
            //判断是否自己所见
            $bookspingfen->private = 1;
            }
            else {
            $bookspingfen->private = 0;
            }
            $bookspingfen->read_state  = 2;  // 1想读 2在读 3读过
            $bookspingfen->update_time    = time();
            $bookspingfen->save();
        }

        //评分信息
        $this->UpdatePingfen($bookid);

        //$bookid = $bookspingfen->book_id;
        return Redirect::to('info/'. $bookid);
    }

    public function saveread()
    {
        //读过 评分
        $uid = Session::get('nameid');
        $bookid = request::input('bookid-read');
        $user = BooksPingfen::where('uid', $uid)->where('book_id', $bookid)->count();
        //会员评论过这本书就更新信息，没有评论过这本书就添加信息
        if($user == 0) {
            // 添加
            $bookspingfen = new BooksPingfen;
            $bookspingfen->book_id     = request::input('bookid-read');
            $bookspingfen->uid         = $uid;
            $bookspingfen->rating      = request::input('rating-read');
            $bookspingfen->memo        = request::input('memo-read');
            if((request::input('private-read'))) {
            //判断是否自己所见
                $bookspingfen->private = 1;
            }
            else {
                $bookspingfen->private = 0;
            }
            $bookspingfen->read_state  = 3;  // 1想读 2在读 3读过
            $bookspingfen->add_time    = time();
            $bookspingfen->save();
        }
        else {
            // 更新
            $bookspingfen = BooksPingfen::where('uid', $uid)->where('book_id',$bookid)->first();
            //$bookspingfen->book_id     = request::input('bookid-read');
            //$bookspingfen->uid         = $uid;
            $bookspingfen->rating      = request::input('rating-read');
            $bookspingfen->memo        = request::input('memo-read');
            if((request::input('private-read'))) {
            //判断是否自己所见
            $bookspingfen->private = 1;
            }
            else {
            $bookspingfen->private = 0;
            }
            $bookspingfen->read_state  = 3;  // 1想读 2在读 3读过
            $bookspingfen->update_time    = time();
            $bookspingfen->save();
        }

        //评分信息
        $this->UpdatePingfen($bookid);

        //$bookid = $bookspingfen->book_id;
        return Redirect::to('info/'. $bookid);
    }

    public function savereadedit() {
        // 修改想读，在读，读过
        $uid = Session::get('nameid');
        $bookid = request::input('bookid-edit');
        $bookspingfen = BooksPingfen::where('uid', $uid)->where('book_id',$bookid)->first();
        //$bookspingfen->book_id     = request::input('bookid-read');
        //$bookspingfen->uid         = $uid;
        $rating =request::input('rating-edit');
        if($rating) {
            $bookspingfen->rating    = request::input('rating-edit');
        }
        else {
            $bookspingfen->rating    = 0;
        }
        $bookspingfen->memo        = request::input('memo-edit');
        if((request::input('private-edit'))) {
        //判断是否自己所见
        $bookspingfen->private = 1;
        }
        else {
        $bookspingfen->private = 0;
        }
        $bookspingfen->read_state  = request::input('read-state');  // 1想读 2在读 3读过
        $bookspingfen->update_time    = time();
        $bookspingfen->save();

        //评分信息
        $this->UpdatePingfen($bookid);

        //$bookid = $bookspingfen->book_id;
        return Redirect::to('info/'. $bookid);
    }

    public function pfremove($bookid) {
        // 删除评分信息
        $uid = Session::get('nameid');
        $bookspingfen = BooksPingfen::where('book_id', $bookid)->where('uid', $uid)->delete();
        //$bookspingfen = BooksPingfen::where('book_id', $bookid)->where('uid', $uid)->first();
        //$bookspingfen->delete();
        //重新统计评分信息
        $this->UpdatePingfen($bookid);
        return Redirect::to('info/'. $bookid); 
    }
    

    public function UpdatePingfen($bookid) {
        //更新本书籍评分的信息(公用函数)
        //评分的人数
        $ren5 = BooksPingfen::where('book_id',$bookid)->where('rating', '5')->count();
        $ren4 = BooksPingfen::where('book_id',$bookid)->where('rating', '4')->count();
        $ren3 = BooksPingfen::where('book_id',$bookid)->where('rating', '3')->count();
        $ren2 = BooksPingfen::where('book_id',$bookid)->where('rating', '2')->count();
        $ren1 = BooksPingfen::where('book_id',$bookid)->where('rating', '1')->count();
        //$ren0 = BooksPingfen::where('book_id',$bookid)->where('rating', '0')->count();

        //评分的总人数
        $rencount = $ren5 + $ren4 + $ren3 + $ren2 + $ren1;
        //每个星级（1，2，3，4，5）的评分人数的百分比
        if($rencount == 0) {
            $star5 = 0;
            $star4 = 0;
            $star3 = 0;
            $star2 = 0;
            $star1 = 0;
            //得分
            $fenshu = 0;
        }
        else {
            $star5 = ($ren5/$rencount)*100;
            $star4 = ($ren4/$rencount)*100;
            $star3 = ($ren3/$rencount)*100;
            $star2 = ($ren2/$rencount)*100;
            $star1 = ($ren1/$rencount)*100;
            //得分
            $fenshu = ($ren5*5 + $ren4*4 + $ren3*3 + $ren2*2 + $ren1*1)/$rencount * 2;
        }       
        
        $pingfeninfo = PingfenInfo::where('book_id',$bookid)->first();
        if(!empty($pingfeninfo)) {
            //有数据更新
            //$pingfengnfo->book_id = $bookid;
            $pingfeninfo->fenshu  = $fenshu;
            $pingfeninfo->rencount  = $rencount;
            $pingfeninfo->star5 = $star5;
            $pingfeninfo->star4 = $star4;
            $pingfeninfo->star3 = $star3;
            $pingfeninfo->star2 = $star2;
            $pingfeninfo->star1 = $star1;
            $pingfeninfo->save();
        }
        else {
            //无数据添加
            $pingfeninfo = new PingfenInfo;
            $pingfeninfo->book_id = $bookid;
            $pingfeninfo->fenshu  = $fenshu;
            $pingfeninfo->rencount  = $rencount;
            $pingfeninfo->star5 = $star5;
            $pingfeninfo->star4 = $star4;
            $pingfeninfo->star3 = $star3;
            $pingfeninfo->star2 = $star2;
            $pingfeninfo->star1 = $star1;
            $pingfeninfo->save();
        }
    }

    public function readinfo($bookid) {
        //读过本书的所有会员
        $books = Book::where('book_id', $bookid)->first();        
        $bookspingfens = BooksPingfen::where('book_id', $bookid)->where('read_state', 3)->get();        
        foreach ($bookspingfens as $bookspingfen) {
            //得到会员资料
            $bookspingfen['users'] = $bookspingfen->hasOnUsersReg;
        }
        $want = BooksPingfen::where('book_id', $bookid)->where('read_state', 1)->get(); //想读
        $reading = BooksPingfen::where('book_id', $bookid)->where('read_state', 2)->get(); //在读
        $read = BooksPingfen::where('book_id', $bookid)->where('read_state', 3)->get(); //读过
        //评分信息
        $pingfeninfo = PingfenInfo::where('book_id', $bookid)->first();
        if(empty($pingfeninfo)) {
            $pf = 0;
        }
        else {
            $pf = 1;
        }
        return view('books.layouts.want_reading_read', array(
            'books' => $books, 
            'bookspingfens' => $bookspingfens, 
            'want' => $want,
            'reading' => $reading,
            'read' => $read,
            'pingfeninfo' => $pingfeninfo,
            'pf' => $pf
            ));
    }

    public function readinginfo($bookid) {
        //在读本书的所有会员
        $books = Book::where('book_id', $bookid)->first();        
        $bookspingfens = BooksPingfen::where('book_id', $bookid)->where('read_state', 2)->get();        
        foreach ($bookspingfens as $bookspingfen) {
            //得到会员资料
            $bookspingfen['users'] = $bookspingfen->hasOnUsersReg;
        }
        $want = BooksPingfen::where('book_id', $bookid)->where('read_state', 1)->get(); //想读
        $reading = BooksPingfen::where('book_id', $bookid)->where('read_state', 2)->get(); //在读
        $read = BooksPingfen::where('book_id', $bookid)->where('read_state', 3)->get(); //读过
        //评分信息
        $pingfeninfo = PingfenInfo::where('book_id', $bookid)->first();
        if(empty($pingfeninfo)) {
            $pf = 0;
        }
        else {
            $pf = 1;
        }
        return view('books.layouts.want_reading_read', array(
            'books' => $books, 
            'bookspingfens' => $bookspingfens, 
            'want' => $want,
            'reading' => $reading,
            'read' => $read,
            'pingfeninfo' => $pingfeninfo,
            'pf' => $pf
            ));
    }

    public function wantinfo($bookid) {
        //想读本书的所有会员
        $books = Book::where('book_id', $bookid)->first();        
        $bookspingfens = BooksPingfen::where('book_id', $bookid)->where('read_state', 1)->get();        
        foreach ($bookspingfens as $bookspingfen) {
            //得到会员资料
            $bookspingfen['users'] = $bookspingfen->hasOnUsersReg;
        }
        $want = BooksPingfen::where('book_id', $bookid)->where('read_state', 1)->get(); //想读
        $reading = BooksPingfen::where('book_id', $bookid)->where('read_state', 2)->get(); //在读
        $read = BooksPingfen::where('book_id', $bookid)->where('read_state', 3)->get(); //读过
        //评分信息
        $pingfeninfo = PingfenInfo::where('book_id', $bookid)->first();
        if(empty($pingfeninfo)) {
            $pf = 0;
        }
        else {
            $pf = 1;
        }
        return view('books.layouts.want_reading_read', array(
            'books' => $books, 
            'bookspingfens' => $bookspingfens, 
            'want' => $want,
            'reading' => $reading,
            'read' => $read,
            'pingfeninfo' => $pingfeninfo,
            'pf' => $pf
            ));
    }

}
